<?php
/*
 * @o: ========================================================
 * @Created By: VsCode
 * @Author: Fu
 * @name: 管理员控制器
 * @LastEditTime: 2021-05-12 15:20:20
 * @Date: 2021-04-02 18:13:40
 * @u: ========================================================
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\goods\Goods as GoodsModel;
use app\admin\model\Order as OrderModel;

use think\Db;
/**
 * 首页接口
 */
class Order extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    
    public function affirmorder()
    {
        $param = input('param.');
        // $data['user_id'] = $param['user_id'];
        // $data['goods'] = $param['goods'];
        // $param['goods'] = ['1' => 3,'2' => 4,];
        // print_r($param);exit;
        $address = Db::name('address')->where('user_id',$param['user_id'])->where('status',1)->find();
        $goods = [];
        $freight = 10;
        foreach($param['goods'] as $key=>$value){
            $res = Db::name('goods')->field('id,goods_name,goods_rice,goods_image')->where('id',$key)->find();
            $res['count_goods_money'] = $res['goods_rice'] * $value;
            $res['count_goods'] =$value;
            $goods[] = $res;
        }
        $count_money = 0;
        foreach($goods as $key=>$value){
            $count_money += $value['count_goods_money'];
        }
        $data = [
            'address' => $address,
            'goods' =>$goods,
            'count_goods_money' => $count_money + $freight,//应付金额
            'cope_with' => $count_money,//应付金额
            'freight' => $freight,//运费
        ];
        $this->success('成功',$data);
    }


    /**
     * @FunctionName: 
     * @Author: Fcy
     * @param {*}
     * @return {*}
     * @Date: 2021-05-12 11:28:01
     */
    public function subpayorder()
    {
        $param = input('param.');
        // $param['goods'] = [
        //     '1' => 3,
        //     '2' => 4
        // ];
        $user = Db::name('user')->where('id',$param['user_id'])->find();//获取用户米粒值余额
        foreach($param['goods'] as $key=>$value){ // 遍历获取商品
            $res = Db::name('goods')->field('id,goods_name,goods_rice,goods_image')->where('id',$key)->find();
            $res['count_goods_money'] = $res['goods_rice'] * $value;
            $res['count_goods'] =$value;
            $goods[] = $res;
        }
        $count_money = 0;
        $freight = 10;
        foreach($goods as $key=>$value){ //获取商品总金额
            $count_money += $value['count_goods_money'];
        }
        
        $residue = $user['score'] - ($count_money + $freight);// 计算 余额可以支付
        if($residue < 0  ){ // 余额不足
            $status = 1;
        }else{// 支付成功  更新用户余额
            $res = Db::name('user')->where('id',$param['user_id'])->update(['score' => $residue]);
            $status = 2;
        }
        $order_id = Db::name('order')->insertGetId([ //插入订单表
            'order_number' => nonceStr(),
            'order_stutas' =>$status,
            'createtime' => time(),
            'user_id' => $param['user_id'],
            'address_id' => $param['address_id']
        ]);

        $n = [];
        foreach($param['goods'] as $key=>$value){ //遍历插入订单子表
            $info = Db::name('goods')->field('id,goods_name,goods_rice,goods_image')->where('id',$key)->find();
            $f['order_id'] = $order_id;
            $f['goods_id'] = $key;
            $f['price'] = $info['goods_rice'];
            $f['goods_num'] = $value;
            $n[] = $f;
        }
        $result = Db::name('order_child')->insertAll($n);//遍历插入订单子表
        if($status == 1){
            $this->success('余额不足');
        }else if($status == 2){
            $this->success('支付成功');
        }
    }
    /**
     * @FunctionName: 
     * @Author: Fcy
     * @param {*}
     * @return {*}
     * @Date: 2021-05-12 09:45:54
     */
    public function orderlist(OrderModel $OrderModel)
    {
        $param = input('param.');
        
        $where['user_id'] = ['eq',$param['user_id']];
        if($param['status'] != 0){  //0是全部 1是待付款2 是待发货3是待收货4是已完成
            $where['order_stutas'] = ['eq',$param['status']];
        }
        
        $orderlist = $OrderModel->with(['ordermay'=>function($qurey){
            return $qurey->with(['goods'=>function($qurey){
                return $qurey->field('id,goods_name,goods_image');
            }]);
        }])->field('*')->where($where)->select()->toArray();
        foreach($orderlist as $key=>$value){
            if($value['order_stutas'] == 1){
                $orderlist[$key]['order_stutas'] = '待付款';
            }
            if($value['order_stutas'] == 2){
                $orderlist[$key]['order_stutas'] = '待发货';
            }
            if($value['order_stutas'] == 3){
                $orderlist[$key]['order_stutas'] = '待收货';
            }
            if($value['order_stutas'] == 4){
                $orderlist[$key]['order_stutas'] = '已完成';
            }
            $count_money = 0;
            foreach($value['ordermay'] as $k=>$v){
               $count_money += $v['goods_num'] * $v['price'];
            }
            $orderlist[$key]['count_money'] = $count_money;
        }
        $this->success('请求成功',$orderlist);
    }


    public function delorder()
    {
        $param = input('param.');
        $res = Db::name('order')->where('id',$param['id'])->delete();
        $this->success('成功',$res);
    }
}
